package net.ahdiaz.rubik.tests;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.xml.bind.JAXBException;

import net.ahdiaz.rubik.model.conf.RubikConfig;
import net.ahdiaz.rubik.sedna.SednaMarshaller;
import net.ahdiaz.rubik.sedna.SednaUnmarshaller;

public class Test_RubikConf {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Test_RubikConf();
	}
	
	public Test_RubikConf() {
		try {
			
			RubikConfig c = this.read();
			this.save(c);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public RubikConfig read() throws JAXBException, FileNotFoundException {
		
		SednaUnmarshaller um = new SednaUnmarshaller(RubikConfig.class);
		Scanner s = new Scanner(new File(System.getProperty("user.dir") + "/resources/conf/rubik.conf.xml"));
		s.useDelimiter("\0");
		RubikConfig c = (RubikConfig) um.unmarshal(s.next());
		
		System.out.println(c.getServer().getUrl());
		System.out.println(c.getServer().getDbname());
		System.out.println(c.getServer().getUser());
		System.out.println(c.getServer().getPass());
		return c;
	}
	
	public void save(RubikConfig c) throws JAXBException {
		c.getServer().setUrl("192.168.2.101");
		c.getServer().setDbname("foobar");
		c.getServer().setUser("rubik");
		String xml = SednaMarshaller.marshal(c).toString();
		System.out.println(xml);
	}
}
